package src.main.原型模式;

public class Subject {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Subject [name=" + name + "]";
    }

    public static void main(String[] args) {
        Subject subject = new Subject();
        subject.setName("数学");
        Teacher teacher1 = new Teacher();
        teacher1.setName("张老师");
        teacher1.setSubject(subject);
        Teacher teacher2 = null;
        try {
            System.out.println("克隆后------------------");
            teacher2 = (Teacher) teacher1.clone();
            System.out.println("teacher 2:" + teacher2);
            System.out.println("改变teacher1后------------------");
            teacher1.getSubject().setName("语文");
            System.out.println("teacher 1:" + teacher1);
            System.out.println("teacher 2:" + teacher2);
        } catch (CloneNotSupportedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

